package prj.carmap.mobile.activity;

import org.apache.http.entity.StringEntity;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.process.SellerTypeProcess;
import prj.carmap.mobile.util.AccessLogUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.miaowang.salad.activity.MineInfoActivity;

/**
 * 更多界面
 *
 */
public class MoreActivity extends SherlockActivity {

	static final String TAG = "MoreActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		CarmapApplication.currentPage = "more";
		setContentView(R.layout.activity_more);
		CarmapApplication.addActivity(this);

		// Inflate the custom view
		View customNav = LayoutInflater.from(this).inflate(R.layout.nav_view, null);
		// Attach to the action bar
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		getSupportActionBar().setCustomView(customNav);
		customNav.setLayoutParams(lp);

		//标题
		((TextView) customNav.findViewById(R.id.nav_title)).setText(R.string.more);

		//返回按钮
		View returnBtn = customNav.findViewById(R.id.return_prev);
		returnBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				MoreActivity.this.finish();
			}
		});

		//退出按钮
		View viewQuit = findViewById(R.id.quit);
		viewQuit.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				exitDialog();
			}
		});

		//用户信息
		/*
		TextView userName = (TextView) findViewById(R.id.user_name);
		TextView telephone = (TextView) findViewById(R.id.telephone);
		if (CarmapApplication.getInstance().userInfo != null) {
			userName.setText(CarmapApplication.getInstance().userInfo.name);
			telephone.setText(CarmapApplication.getInstance().userInfo.phone);
		}
		*/

		//版本信息
		PackageInfo info = getVersionCode(this);
		String version = "当前版本: v" + info.versionName + " Build " + info.versionCode;
		TextView versionInfo = (TextView) findViewById(R.id.version);
		versionInfo.setText(version);

		//
		initMenuItem();
		initActionBar();
	}

	protected void exitDialog() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setMessage("确定要退出吗?");
		dialog.setTitle("提示");
		dialog.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						for (Activity activity : CarmapApplication.activities) {
							if (!activity.isFinishing()) {
								activity.finish();
							}
						}
						//发送退出log给服务器
						StringEntity log = AccessLogUtil.createAccessLogTypeParam(3);
						if (log != null)
							new AsyncHttpClient().post(MoreActivity.this, CarmapApplication.SERVER_URL + "/rest/accesslog", log, "application/json", new AsyncHttpResponseHandler());
						System.exit(0);
					}
				});
		dialog.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		dialog.create().show();
	}

	@SuppressWarnings("deprecation")
	private void initMenuItem() {
		if ("type".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_type);
			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this.findViewById(R.id.item_type_image);
			itemImage.setImageResource(R.drawable.type_select);
			// 底部菜单文字
			TextView itemText = (TextView) this.findViewById(R.id.item_type_text);
			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		}
//		else if ("search".equals(CarmapApplication.currentPage)) {
//			// 底部菜单项
//			View menuItem = this.findViewById(R.id.menu_item_search);
//			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
//			// 底部菜单图标
//			ImageView itemImage = (ImageView) this.findViewById(R.id.item_search_image);
//			itemImage.setImageResource(R.drawable.search_select);
//			// 底部菜单文字
//			TextView itemText = (TextView) this.findViewById(R.id.item_search_text);
//			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
//		}
		else if ("mine".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_mine);
			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this.findViewById(R.id.item_mine_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this.findViewById(R.id.item_mine_text);
			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		}
		else if ("salad".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_salad);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_salad_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_salad_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
//		else if ("more".equals(CarmapApplication.currentPage)) {
//			// 底部菜单项
//			View menuItem = this.findViewById(R.id.menu_item_more);
//			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
//			// 底部菜单图标
//			ImageView itemImage = (ImageView) this.findViewById(R.id.item_more_image);
//			itemImage.setImageResource(R.drawable.more_select);
//			// 底部菜单文字
//			TextView itemText = (TextView) this.findViewById(R.id.item_more_text);
//			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
//		}
	}


	private void initActionBar() {
		Log.d(TAG, "initActionBar");
		
		View view = this.findViewById(R.id.menu_item_home);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				SellerTypeProcess process = new SellerTypeProcess();
//				process.setContext(MoreActivity.this);
//				process.getSellerTypes();
			}
		});

		 view = this.findViewById(R.id.menu_item_type);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SellerTypeProcess process = new SellerTypeProcess();
				process.setContext(MoreActivity.this);
				process.getSellerTypes();
			}
		});

		view = this.findViewById(R.id.menu_item_mine);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				UserInfo userInfo = CarmapApplication.getInstance().userInfo;
//				if (userInfo == null) {
//					MoreActivity.this.startActivity(new Intent(MoreActivity.this, LoginActivity.class));
//				}
//				else {
//					MoreActivity.this.startActivity(new Intent(MoreActivity.this, ProfileActivity.class));
//				}
				MoreActivity.this.startActivity(new Intent(MoreActivity.this, MineInfoActivity.class));
			}
		});

//		view = this.findViewById(R.id.menu_item_search);
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				CarmapApplication.popView = new DialogView(MoreActivity.this);
//				CarmapApplication.popView.show();
//				MoreActivity.this.startActivity(new Intent(MoreActivity.this, SellerSearchActivity.class));
//			}
//		});
	}

	private PackageInfo getVersionCode(Context context) {
		PackageManager manager = context.getPackageManager();
		PackageInfo info;
		try {
			info = manager.getPackageInfo(context.getPackageName(), 0);
			return info;
		}
		catch (NameNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		CarmapApplication.currentPage = "more";
	}

}
